package com.wit.witmg.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class WxCheckUtil {
    @Resource
    private RedisUtil redisUtil;
    @Value("${WX.wx_access_token_url}")
    private String wx_access_token_url;
    @Value("${WX.wx_appid}")
    private String wx_appid;
    @Value("${WX.wx_secret}")
    private String wx_secret;

    /**
     *  纯文本拦截敏感词
     * @param textConetnt
     * @return
     */
    public boolean checkText(String textConetnt) {
        try {
            String accessToken = "";
            if (StringUtils.instance.isEmpty(redisUtil.get("access_token"))) {
                Map map = new HashMap();
                map.put("grant_type", "client_credential");
                map.put("appid", wx_appid);
                map.put("secret", wx_secret);
                String ret = HttpUtils.instance.doGet(wx_access_token_url, map);
                JSONObject objret = JSON.parseObject(ret);
                if (objret.containsKey("access_token")) {
                    accessToken = objret.getString("access_token");
                    redisUtil.set("access_token", accessToken, 2 * 60 * 60);//有效时间2小时
                }
            } else {
                accessToken = redisUtil.get("access_token").toString();
            }
            CloseableHttpClient httpclient = HttpClients.createDefault();
            CloseableHttpResponse response = null;
            HttpPost request = new HttpPost("https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" + accessToken);
            request.addHeader("Content-Type", "application/json;charset=UTF-8");
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("content", textConetnt);
            request.setEntity(new StringEntity(JSONObject.toJSONString(paramMap), ContentType.create("application/json", "utf-8")));

            response = httpclient.execute(request);
            HttpEntity httpEntity = response.getEntity();
            String result = EntityUtils.toString(httpEntity, "UTF-8");// 转成string
            System.out.println(result);
            JSONObject jso = JSONObject.parseObject(result);

            Object errcode = jso.get("errcode");
            int errCode = (int) errcode;
            if (errCode == 0) {
                return true;
            } else if (errCode == 87014) {
                System.out.println("内容违规-----------" + textConetnt);
                return false;
            }

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("----------------调用腾讯内容过滤系统出错------------------");
            return true;
        }
    }
}
